
function AssetManager() {
    /**
     * 生成一个不重复的ID
     * @param { Number } randomLength
     */
    function getUuiD(randomLength) {
        return Number(Math.random().toString().substr(2, randomLength) + Date.now()).toString(36)
    }

    let fileHandle;
    let currentDir = "";

    this.config = {
        layout : {
            name : 'assetManager',
            padding : 0,
            panels: [
                { type: 'left', size: 200, resizable: true, minSize: 120 },
                { type: 'main', minSize: 550, overflow: 'hidden' },
                { type: 'right', size: 400, resizable: false, html:'preview'},
            ]
        },
        fileSidebar : {
            name : 'fileSidebar',
        },
        fileGrid : {
            name : 'fileGrid',
            columns : [
                { field: 'name', text: 'name', size: '50%' },
                { field: 'type', text: 'type', size: '50%' },
                ],
            records: [
            ]
        }
    }

    this.openFolder = async function() {

        //清空目录
        w2ui.fileSidebar.nodes = [];
        w2ui.fileSidebar.refresh();

        fileHandle = await window.showDirectoryPicker();
        for await (const entry of fileHandle.values()) {
            //console.log(entry.kind, entry.name);
            let id = '/';
            if (entry.kind === 'directory') {
                //使用路径做唯一id
                let newId = id + entry.name;
                w2ui.fileSidebar.add([{id: newId, text: entry.name, img: 'icon-folder'}]);
                let nodes = w2ui.fileSidebar.nodes;
                let newNode = w2ui.fileSidebar.get(newId);
                //newNode.sidebar.on('click', async function (event) {
                await renderFolder(newNode, fileHandle, entry.name, newId);
                    //var newDirHandle = await fileHandle.getDirectoryHandle(entry.name, {create: false});
                    //renderFiles(newDirHandle);
                //});
            }
        }

        w2ui.fileSidebar.on('dblClick', async function(event) {
            console.log(event.target);
            await renderFiles(fileHandle, event.target);
        });
        await renderFiles(fileHandle, currentDir);
    }

    async function renderFolder(parent, dirHandle, name, parentId) {
        const subDirHandle = await dirHandle.getDirectoryHandle(name, {create: false});
        for await (const entry of subDirHandle.values()) {
            //console.log(entry.kind, entry.name);
            if (entry.kind === 'directory') {
                let newId = parentId + '/' + entry.name;
                //防止add时疯狂报error
                var findRes = parent.sidebar.find({id: newId});
                if (findRes.length !== 0)
                    return;
                parent.sidebar.add(parent, [{id: newId, text: entry.name, img: 'icon-folder',}]);
                let nodes = parent.sidebar.nodes;
                let newNode = parent.sidebar.get(newId);

                await renderFolder(newNode, subDirHandle, entry.name, parentId);
                // newNode.sidebar.on('click', async function (event) {
                //     event.preventDefault();
                //     var newDirHandle = await subDirHandle.getDirectoryHandle(entry.name, {create: false});
                //     renderFiles(newDirHandle);
                // });
            }
        }
    }

    async function renderFiles(dirHandle, currentDir) {

        w2ui.fileGrid.records = [];
        w2ui.fileGrid.refresh();

        const arrayOfDir = currentDir.split('/');
        let subDirHandle = dirHandle;
        for (let i=1; i<arrayOfDir.length; i++){
            subDirHandle = await subDirHandle.getDirectoryHandle(arrayOfDir[i], {create: false});
        }
        let id = 0;
        for await (const entry of subDirHandle.values()) {
            w2ui.fileGrid.add([{recid : id++, name : entry.name, type : entry.kind}]);
        }
    }
}

export {AssetManager};